﻿<Project DefaultTargets="Restore;Build">
  <Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />

  <PropertyGroup>
    <TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <IncludeBuildOutput>false</IncludeBuildOutput>

    <WorkloadIntermediateOutputPath>$(ArtifactsObjDir)workloads/</WorkloadIntermediateOutputPath>
    <VSTemp>$(WorkloadIntermediateOutputPath)VS/</VSTemp>
    <WorkloadOutputPath>$(ArtifactsBinDir)workloads/</WorkloadOutputPath>
    <WorkloadOutputPath Condition="'$(workloadArtifactsPath)' != ''">$(workloadArtifactsPath)/</WorkloadOutputPath>
    <PackageSource>$(ArtifactsShippingPackagesDir)</PackageSource>
    <PackageSource Condition="'$(workloadPackagesPath)' != ''">$(workloadPackagesPath)/</PackageSource>
    <RunAnalyzers>false</RunAnalyzers>
  </PropertyGroup>

  <!-- Arcade -->
  <PropertyGroup>
    <!-- Temp directory for light command layouts -->
    <LightCommandObjDir>$(ArtifactsObjDir)/LightCommandPackages</LightCommandObjDir>
    <!-- Directory for the zipped up light command package -->
    <LightCommandPackagesDir>$(ArtifactsNonShippingPackagesDir)</LightCommandPackagesDir>
  </PropertyGroup>

  <PropertyGroup>
    <WixToolsetPath>$(PkgMicrosoft_Signed_Wix)\tools</WixToolsetPath>
    <SwixPluginPath>$(PkgMicroBuild_Plugins_SwixBuild_Dotnet)</SwixPluginPath>
    <SwixBuildTargets>$(SwixPluginPath)\build\MicroBuild.Plugins.SwixBuild.targets</SwixBuildTargets>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.Build.Tasks.Workloads" Version="$(MicrosoftDotNetBuildTasksWorkloadsPackageVersion)" GeneratePathProperty="true" />
    <PackageReference Include="Microsoft.Signed.WiX" Version="$(MicrosoftSignedWixVersion)" GeneratePathProperty="true" />
    <PackageReference Include="MicroBuild.Plugins.SwixBuild.Dotnet" Version="$(SwixPackageVersion)" GeneratePathProperty="true" />
    <PackageReference Include="Microsoft.DotNet.Build.Tasks.Installers" Version="$(MicrosoftDotNetBuildTasksInstallersVersion)" GeneratePathProperty="true" />
  </ItemGroup>

  <Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" />

  <Target Name="Build" DependsOnTargets="GetAssemblyVersion;_GetVersionProps;_GenerateMsiVersionString">
    <ItemGroup>
      <!-- Overrides for Visual Studio setup generation. If the workload definition IDs change,
           these must be updated. -->
      <_ComponentResources Include="microsoft-net-runtime-mono-tooling" Title=".NET Shared Mobile Build Tools"
                          Description="Shared build tasks for mobile platform development."/>
      <_ComponentResources Include="wasm-tools" Title=".NET WebAssembly Build Tools"
                          Description="Build tools for WebAssembly ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="wasm-experimental" Title=".NET WebAssembly Experimental Tools"
                          Description=".NET WebAssembly experimental tooling"/>
      <_ComponentResources Include="wasi-experimental" Title=".NET Wasi Experimental"
                          Description=".NET Experimental SDK and tooling for WASI"/>
      <_ComponentResources Include="microsoft-net-runtime-android" Title=".NET Android Build Tools"
                          Description="Build tools for Android compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-android-aot" Title=".NET Android Build Tools (AoT)"
                          Description="Build tools for Android ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-ios" Title=".NET iOS Build Tools"
                          Description="Build tools for iOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-tvos" Title=".NET tvOS Build Tools"
                          Description="Build tools for tvOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-maccatalyst" Title=".NET Mac Catalyst Build Tools"
                          Description="Build tools for Mac Catalyst compilation and native linking."/>
      <_ComponentResources Include="runtimes-ios" Title=".NET iOS Runtimes"
                          Description=".NET runtime components for iOS execution."/>
      <_ComponentResources Include="runtimes-tvos" Title=".NET tvOS Build Tools"
                          Description=".NET runtime components for tvOS execution."/>
      <_ComponentResources Include="runtimes-maccatalyst" Title=".NET Mac Catalyst Build Tools"
                          Description=".NET runtime components for Mac Catalyst execution."/>
      <_ComponentResources Include="runtimes-windows" Title=".NET Windows Runtimes"
                          Description=".NET runtime components for Windows execution."/>

      <_ComponentResources Include="microsoft-net-runtime-mono-tooling-net7" Title=".NET 7.0 Shared Mobile Build Tools"
                          Description="Shared build tasks for mobile platform development."/>
      <_ComponentResources Include="wasm-tools-net7" Title=".NET 7.0 WebAssembly Build Tools"
                          Description="Build tools for net6.0 WebAssembly ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-android-net7" Title=".NET 7.0 Android Build Tools"
                          Description="Build tools for net6.0 Android compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-android-aot-net7" Title=".NET 7.0 Android Build Tools (AoT)"
                          Description="Build tools for net6.0 Android ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-ios-net7" Title=".NET 7.0 iOS Build Tools"
                          Description="Build tools for net7.0 iOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-tvos-net7" Title=".NET 7.0 tvOS Build Tools"
                          Description="Build tools for net7.0 tvOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-maccatalyst-net7" Title=".NET 7.0 Mac Catalyst Build Tools"
                          Description="Build tools for net7.0 Mac Catalyst compilation and native linking."/>
      <_ComponentResources Include="runtimes-ios-net7" Title=".NET 7.0 iOS Runtimes"
                          Description=".NET 7.0 runtime components for iOS execution."/>
      <_ComponentResources Include="runtimes-tvos-net7" Title=".NET 7.0 tvOS Build Tools"
                          Description=".NET 7.0 runtime components for tvOS execution."/>
      <_ComponentResources Include="runtimes-maccatalyst-net7" Title=".NET 7.0 Mac Catalyst Build Tools"
                          Description=".NET 7.0 runtime components for Mac Catalyst execution."/>
      <_ComponentResources Include="runtimes-windows-net7" Title=".NET 7.0 Windows Runtimes"
                          Description=".NET 7.0 runtime components for Windows execution."/>

      <_ComponentResources Include="microsoft-net-runtime-mono-tooling-net6" Title=".NET 6.0 Shared Mobile Build Tools"
                          Description="Shared build tasks for mobile platform development."/>
      <_ComponentResources Include="wasm-tools-net6" Title=".NET 6.0 WebAssembly Build Tools"
                          Description="Build tools for net6.0 WebAssembly ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-android-net6" Title=".NET 6.0 Android Build Tools"
                          Description="Build tools for net6.0 Android compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-android-aot-net6" Title=".NET 6.0 Android Build Tools (AoT)"
                          Description="Build tools for net6.0 Android ahead-of-time (AoT) compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-ios-net6" Title=".NET 6.0 iOS Build Tools"
                          Description="Build tools for net6.0 iOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-tvos-net" Title=".NET 6.0 tvOS Build Tools"
                          Description="Build tools for net6.0 tvOS compilation and native linking."/>
      <_ComponentResources Include="microsoft-net-runtime-maccatalyst-net6" Title=".NET 6.0 Mac Catalyst Build Tools"
                          Description="Build tools for net6.0 Mac Catalyst compilation and native linking."/>
      <_ComponentResources Include="runtimes-ios-net6" Title=".NET 6.0 iOS Runtimes"
                          Description=".NET 6.0 runtime components for iOS execution."/>
      <_ComponentResources Include="runtimes-tvos-net6" Title=".NET 6.0 tvOS Build Tools"
                          Description=".NET 6.0 runtime components for tvOS execution."/>
      <_ComponentResources Include="runtimes-maccatalyst-net6" Title=".NET 6.0 Mac Catalyst Build Tools"
                          Description=".NET 6.0 runtime components for Mac Catalyst execution."/>
      <_ComponentResources Include="runtimes-windows-net6" Title=".NET 6.0 Windows Runtimes"
                          Description=".NET 6.0 runtime components for Windows execution."/>

      <ComponentResources Include="@(_ComponentResources)" Version="$(FileVersion)"/>
    </ItemGroup>

    <!-- BAR requires having version information in blobs -->
    <PropertyGroup>
      <VersionedVisualStudioSetupInsertionPath>$(VisualStudioSetupInsertionPath)$(SDKBundleVersion)\</VersionedVisualStudioSetupInsertionPath>
    </PropertyGroup>

    <!-- Shorten package names to avoid long path issues in Visual Studio -->
    <ItemGroup>
      <!-- These need to be in order - matching rules will try in order. -->
      <ShortNames Include="Microsoft.NET.Workload.Mono.ToolChain.net">
        <Replacement>Mono.</Replacement>
      </ShortNames>
      <ShortNames Include="Microsoft.NET.Workload.Mono.ToolChain.Current.">
        <Replacement>Mono.</Replacement>
      </ShortNames>
      <ShortNames Include="microsoft.netcore.app.runtime;Microsoft.NETCore.App.Runtime;microsoft.net.runtime;Microsoft.NET.Runtime">
        <Replacement>Microsoft</Replacement>
      </ShortNames>
      <ShortNames Include="Microsoft.NET.Workload;microsoft.net.workload">
        <Replacement>Microsoft</Replacement>
      </ShortNames>
    </ItemGroup>

    <ItemGroup>
      <ManifestPackages Include="$(PackageSource)Microsoft.NET.Workload.Mono.ToolChain.*Manifest-%(WorkloadSdkBandVersions.Identity)*.*.nupkg"
                        MsiVersion="$(MsiVersion)"
                        SupportsMachineArch="%(SupportsMachineArch)" />
    </ItemGroup>

    <CreateVisualStudioWorkload BaseIntermediateOutputPath="$(WorkloadIntermediateOutputPath)"
                                AllowMissingPacks="True"
                                BaseOutputPath="$(WorkloadOutputPath)"
                                EnableSideBySideManifests="true"
                                ComponentResources="@(ComponentResources)"
                                PackageSource="$(PackageSource)"
                                ShortNames="@(ShortNames)"
                                WorkloadManifestPackageFiles="@(ManifestPackages)"
                                WixToolsetPath="$(WixToolsetPath)"
                                ManifestMsiVersion="$(MsiVersion)">
      <Output TaskParameter="SwixProjects" ItemName="SwixProjects" />
      <Output TaskParameter="Msis" ItemName="Msis" />
    </CreateVisualStudioWorkload>

    <!-- Split SWIX projects for packs and components/manifests and build them into separate folders. This allows us to consume pack-only drops
         across multiple VS builds to support multi-targeting. -->
    <ItemGroup>
      <SwixWorkloadPackProjects Include="@(SwixProjects)" Condition="'%(PackageType)' == 'msi-pack'"
                                ManifestOutputPath="$(VStemp)\p\%(SwixProjects.SdkFeatureBand)"
                                ZipFile="Workload.VSDrop.mono.net.$(MajorVersion).$(MinorVersion)-%(SwixProjects.SdkFeatureBand).packs.zip"/>
      <SwixComponentsAndManifests Include="@(SwixProjects)" Condition="('%(PackageType)' == 'msi-manifest') Or ('%(PackageType)' == 'manifest-package-group') Or ('%(PackageType)' == 'component' And '%(IsPreview)' == 'false')"
                                  ManifestOutputPath="$(VStemp)\c\%(SwixProjects.SdkFeatureBand)"
                                  ZipFile="Workload.VSDrop.mono.net.$(MajorVersion).$(MinorVersion)-%(SwixProjects.SdkFeatureBand).components.zip"/>
      <SwixPreviewComponentsAndManifests Include="@(SwixProjects)" Condition="('%(PackageType)' == 'msi-manifest') Or ('%(PackageType)' == 'component' And '%(IsPreview)' == 'true')"
                                         ManifestOutputPath="$(VStemp)\c\%(SwixProjects.SdkFeatureBand).pre"
                                         ZipFile="Workload.VSDrop.mono.net.$(MajorVersion).$(MinorVersion)-%(SwixProjects.SdkFeatureBand)-pre.components.zip"/>
      <PartitionedSwixProjects Include="@(SwixWorkloadPackProjects);@(SwixComponentsAndManifests);@(SwixPreviewComponentsAndManifests)" />
    </ItemGroup>

    <!-- Can't build in parallel to the same output folder because of a shared file from the SWIX compiler. -->
    <MSBuild Projects="@(PartitionedSwixProjects)" Properties="SwixBuildTargets=$(SwixBuildTargets);ManifestOutputPath=%(ManifestOutputPath)"/>

    <!-- Create the zip files used for VSDROP creation. -->
    <ItemGroup>
      <SdkFeatureBand Include="%(SwixProjects.SdkFeatureBand)" />
    </ItemGroup>

    <ItemGroup>
      <VSDrop Include="%(PartitionedSwixProjects.ZipFile)" SourceDirectory="%(ManifestOutputPath)" />
    </ItemGroup>

    <MakeDir Directories="$(ArtifactsNonShippingPackagesDir)" />
    <MakeDir Directories="$(VisualStudioSetupInsertionPath)" />
    <ZipDirectory Overwrite="true" SourceDirectory="%(SourceDirectory)"
                  DestinationFile="$(VisualStudioSetupInsertionPath)%(VSDrop.Identity)" />

    <!-- Gather .wixobj files for post-build signing. We'll have to batch since we generated multiple MSIs in the previous step. -->
    <MSBuild Projects="$(MSBuildProjectFile)" Properties="_WixObjDir=%(Msis.WixObj);_Msi=%(Msis.Identity)" Targets="CreateWixPack" />

    <!-- Build all the MSI payload packages for NuGet. -->
    <ItemGroup>
      <MsiPackageProjects Include="%(Msis.PackageProject)" />
    </ItemGroup>

    <!-- We disable PackageValidation which runs because these projects import the repo's Directory.Build.props and Directory.Build.targets file. -->
    <MSBuild Projects="@(MsiPackageProjects)" Properties="OutputPath=$(ArtifactsShippingPackagesDir);IncludeSymbols=false;EnablePackageValidation=false" Targets="restore;pack" />
  </Target>

  <!-- Target to create a single wixpack for signing -->
  <Target Name="CreateWixPack">
    <ItemGroup>
      <_WixObj Include="$(_WixObjDir)\**\*.wixobj" />
    </ItemGroup>

    <CreateLightCommandPackageDrop
      LightCommandWorkingDir="$(LightCommandObjDir)"
      OutputFolder="$(LightCommandPackagesDir)"
      NoLogo="true"
      Cultures="en-us"
      InstallerFile="$(_Msi)"
      WixExtensions="WixUIExtension;WixDependencyExtension;WixUtilExtension"
      WixSrcFiles="@(_WixObj)">
      <Output TaskParameter="OutputFile" PropertyName="_LightCommandPackageNameOutput" />
    </CreateLightCommandPackageDrop>
  </Target>

  <!-- These are just individual targets for testing local builds. -->
  <Target Name="BuildSwixProjects">
    <ItemGroup>
      <SwixProjects Include="$(WorkloadIntermediateOutputPath)**\*.swixproj" />
    </ItemGroup>

    <MSBuild Projects="@(SwixProjects)" BuildInParallel="true" Properties="SwixBuildTargets=$(SwixBuildTargets);ManifestOutputPath=$(VisualStudioSetupInsertionPath)" />
  </Target>

  <Target Name="_GetVersionProps">
    <PropertyGroup>
      <_MajorVersion>$([System.Version]::Parse('$(AssemblyVersion)').Major)</_MajorVersion>
      <_MinorVersion>$([System.Version]::Parse('$(AssemblyVersion)').Minor)</_MinorVersion>
      <_PatchVersion>$([System.Version]::Parse('$(AssemblyVersion)').Build)</_PatchVersion>
      <_BuildNumber>$([System.Version]::Parse('$(AssemblyVersion)').Revision)</_BuildNumber>
    </PropertyGroup>
  </Target>

  <Target Name="_GenerateMsiVersionString">
    <PropertyGroup>
      <VersionPadding Condition="'$(VersionPadding)'==''">5</VersionPadding>
      <!-- Using the following default comparison date will produce versions that align with our internal build system. -->
      <VersionComparisonDate Condition="'$(VersionComparisonDate)'==''">1996-04-01</VersionComparisonDate>
    </PropertyGroup>

    <GenerateCurrentVersion
      SeedDate="$([System.DateTime]::Now.ToString(yyyy-MM-dd))"
      OfficialBuildId="$(OfficialBuildId)"
      ComparisonDate="$(VersionComparisonDate)"
      Padding="$(VersionPadding)">
      <Output PropertyName="BuildNumberMajor" TaskParameter="GeneratedVersion" />
      <Output PropertyName="BuildNumberMinor" TaskParameter="GeneratedRevision" />
    </GenerateCurrentVersion>

    <GenerateMsiVersion
      Major="$(_MajorVersion)"
      Minor="$(_MinorVersion)"
      Patch="$(_PatchVersion)"
      BuildNumberMajor="$(BuildNumberMajor)"
      BuildNumberMinor="$(BuildNumberMinor)">
      <Output TaskParameter="MsiVersion" PropertyName="MsiVersion" />
    </GenerateMsiVersion>
  </Target>
</Project>
